package com.cyan.app.workflow.activity.SFC;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.cyan.app.workflow.AppContext;
import com.cyan.app.workflow.R;
import com.cyan.app.workflow.bean.DDPoolListBean;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.cyan.app.workflow.customview.SwipeListLayout;

import android.os.Bundle;
/**
 * Created by DXW on 2016/12/22.
 * 滴滴物流数据处理类
 */
public class DDDataAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Handler mHandler;
    private Context context;
    private Set<SwipeListLayout> openList = new HashSet();
    private List<DDPoolListBean.DDPoolBean> mPoolList = new ArrayList<DDPoolListBean.DDPoolBean>();

    public DDDataAdapter(Context context, Handler mHandler, List<DDPoolListBean.DDPoolBean> poolList) {
        if (mPoolList != null) {
            mPoolList = poolList;
        }
        this.mHandler = mHandler;
        mInflater = (LayoutInflater) AppContext.getInstance().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.context = context;
    }

    public void setDatas(List<DDPoolListBean.DDPoolBean> poolList) {
        if (poolList != null) {
            mPoolList = poolList;
        }
    }

    @Override
    public int getCount() {
        return mPoolList == null ? 0 : mPoolList.size();
    }

    @Override
    public Object getItem(int position) {
        return mPoolList == null ? null : mPoolList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.sfc_dd_tab1, null);
            viewHolder = new ViewHolder();
            viewHolder.ProssName = (TextView) convertView.findViewById(R.id.dd_tabl_prosscode);
            viewHolder.Selected = (Button) convertView.findViewById(R.id.dd_tabl_selected);
            viewHolder.LeftLayout = (View) convertView.findViewById(R.id.dd_tab1_left_layout);
            viewHolder.RightLayout = (View) convertView.findViewById(R.id.dd_tab1_right_layout);

            viewHolder.Manno = (TextView) convertView.findViewById(R.id.dd_tab1_manno);
            viewHolder.Itnbr = (TextView) convertView.findViewById(R.id.dd_tab1_itnbr);
            viewHolder.Itdsc = (TextView) convertView.findViewById(R.id.dd_tab1_itdsc);
            viewHolder.Manqty = (TextView) convertView.findViewById(R.id.dd_tab1_manqty);
            viewHolder.NextPross = (TextView) convertView.findViewById(R.id.dd_tab1_nextpross);
            viewHolder.Userno = (TextView) convertView.findViewById(R.id.dd_tab1_userno);
            viewHolder.Date = (TextView) convertView.findViewById(R.id.dd_tab1_date);
            viewHolder.SwipeLayout = (SwipeListLayout) convertView.findViewById(R.id.sll_main);
            //viewHolder.Waste = (TextView) convertView.findViewById(R.id.dd_tab1_waste);
            viewHolder.Delete = (TextView) convertView.findViewById(R.id.dd_tab1_delete);
            viewHolder.Type = (TextView) convertView.findViewById(R.id.dd_tab1_type);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.SwipeLayout.setSwipeChangeListener(new SwipeListLayout.OnSwipeChangeListener() {
            @Override
            public void onDraging(SwipeListLayout mSwipeLayout) {
                if (DDActivity.FILTER_SUB_TYPE_LIVE_WL == DDActivity.mFilterSubType) {
                    mSwipeLayout.close();
                }
            }

            @Override
            public void onOpen(SwipeListLayout mSwipeLayout) {
                openList.add(mSwipeLayout);
            }

            @Override
            public void onClose(SwipeListLayout mSwipeLayout) {
                openList.remove(mSwipeLayout);
            }

            @Override
            public void onStartOpen(SwipeListLayout mSwipeLayout) {
                for (SwipeListLayout layout : openList) {
                    layout.close();
                }
                openList.clear();
            }

            @Override
            public void onStartClose(SwipeListLayout mSwipeLayout) {

            }
        });
        final DDPoolListBean.DDPoolBean obj = mPoolList.get(position);
        viewHolder.ProssName.setText(obj.getProssName());
        viewHolder.Manno.setText(obj.getManno());
        viewHolder.Manqty.setText(obj.getOnhand1().toString());
        viewHolder.Itnbr.setText(obj.getItnbr());
        String itdsc=obj.getItdsc();
        if(itdsc.length()>15){
            itdsc=itdsc.substring(0,15);
        }
        viewHolder.Itdsc.setText(itdsc);
        if(obj.getType()==1){
            viewHolder.Type.setText("良品");
            //viewHolder.Type.setBackgroundColor(Color.parseColor("#f1f1f1"));
            viewHolder.Manqty.setTextColor(Color.parseColor("#60ca82"));
        }else if(obj.getType()==2){
            viewHolder.Type.setText("不良返修");
            //viewHolder.Type.setBackgroundColor(Color.parseColor("#c0ffbd21"));
            viewHolder.Manqty.setTextColor(Color.parseColor("#4a86e8"));
        }else if(obj.getType()==3){
            viewHolder.Type.setText("报废品");
            //viewHolder.Type.setBackgroundColor(Color.parseColor("#c099cc00"));
            viewHolder.Manqty.setTextColor(Color.parseColor("#fe5052"));
        }

        viewHolder.Selected.setSelected(DDActivity.selectedMap.containsKey(obj.getPoolId()));

        if (DDActivity.FILTER_SUB_TYPE_LIVE_WL == DDActivity.mFilterSubType) {
            viewHolder.Userno.setText(obj.getUsername());
            viewHolder.Date.setText(obj.getKeyindate());
            viewHolder.Selected.setBackgroundResource(R.drawable.boz01);
            viewHolder.LeftLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //抢单 即时物流移除 ，我的物流增加
                    viewHolder.Selected.setSelected(!viewHolder.Selected.isSelected());
                    //if (viewHolder.Selected.isSelected()) {
                        //viewHolder.Selected.setBackgroundResource(R.drawable.boz02);
                    //} else {
                        //viewHolder.Selected.setBackgroundResource(R.drawable.boz01);
                    //}

                    Message message = new Message();
                    message.what = DDActivity.REFRESH_GRAB;
                    message.obj = obj.getPoolId();
                    mHandler.sendMessage(message);
                }
            });
            viewHolder.NextPross.setVisibility(View.GONE);
        } else if (DDActivity.FILTER_SUB_TYPE_MY_WL == DDActivity.mFilterSubType) {
            viewHolder.Userno.setText(obj.getWlUsername());
            viewHolder.Date.setText(obj.getWlTime());
            viewHolder.NextPross.setVisibility(View.VISIBLE);
            viewHolder.NextPross.setText(obj.getNextProssName());
            //viewHolder.Selected.setBackground(R.drawable.list_selector);
            viewHolder.Selected.setBackgroundResource(R.drawable.list_selector);
            viewHolder.LeftLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    viewHolder.Selected.setSelected(!viewHolder.Selected.isSelected());
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("selectedPool",(DDPoolListBean.DDPoolBean)obj);
                    Message message = new Message();
                    message.what = DDActivity.REFRESH_SELECTED;
                    message.arg1 = viewHolder.Selected.isSelected() ? 1 : 0;
                    message.obj = obj.getPoolId();
                    message.setData(bundle);
                    mHandler.sendMessage(message);
                }
            });
        /*    viewHolder.Waste.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Message message = new Message();
                    message.what = DDActivity.REFRESH_WASTE;
                    message.obj = obj.getPoolId();
                    mHandler.sendMessage(message);
                }
            });*/
            viewHolder.Delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Message message = new Message();
                    message.what = DDActivity.REFRESH_DELETE;
                    message.obj = obj.getPoolId();
                    mHandler.sendMessage(message);
                }
            });
        }
        return convertView;
    }

    class ViewHolder {
        SwipeListLayout SwipeLayout;
        protected TextView ProssName;
        protected Button Selected;
        protected View LeftLayout;
        protected View RightLayout;

        protected TextView Manno;
        protected TextView Itnbr;
        protected TextView Itdsc;
        protected TextView Manqty;
        protected TextView NextPross;
        protected TextView Userno;

        protected TextView Date;
        protected TextView Waste;
        protected TextView Delete;
        protected TextView Type;
    }
}
